package com.company.project.web.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.common.enums.RestCodeEnum;
import com.company.project.common.utils.JsonUtils;
import com.company.project.common.utils.RestUtil;
import com.company.project.common.pojo.Rest;
import com.company.project.web.system.pojo.handcard.Handcard;
import com.company.project.web.system.pojo.handcard.HandcardSelectVO;
import com.company.project.web.system.service.HandcardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 任建波
 * @since 2019-09-09
 */
@Api(value = "手牌", tags = "手牌")
@Controller
@RequestMapping("/system/handcard")
public class HandcardController {

    @Autowired
    private HandcardService handcardService;

    @ApiOperation(value = "获取手牌列表")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", name = "xxxToken", value = "请求头token", required = true, dataType = "String"),
        @ApiImplicitParam(paramType="query", name = "currentPage", value = "页码", required = true, dataType = "Integer"),
        @ApiImplicitParam(paramType="query", name = "status", value = "状态", dataType = "Integer")
    })
    @RequestMapping(value = "getHandcardList", method = RequestMethod.GET)
    @ResponseBody
    public Rest<IPage<HandcardSelectVO>> getHandcardList(Integer currentPage, Integer status) {
        QueryWrapper<Handcard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isdelete", 0);
        IPage<Handcard> iPage = handcardService.page(new Page<>(1, 50), queryWrapper);

        // 查询最大编号
//        QueryWrapper<Handcard> oneWrapper = new QueryWrapper<>();
//        oneWrapper.select("max(code) as code");
//        Handcard handcard = handcardService.getOne(oneWrapper);

        List<HandcardSelectVO> handcardSelectVOList = JsonUtils.jsonToList(iPage.getRecords(), HandcardSelectVO.class);
        IPage<HandcardSelectVO> newIPage = new Page<>();
        newIPage.setRecords(handcardSelectVOList);

        return RestUtil.rest(RestCodeEnum.CODE_200.getCode(), RestCodeEnum.CODE_200.getDesc(), newIPage);
    }

    @ApiOperation(value = "批量添加手牌")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", name = "xxxToken", value = "请求头token", required = true, dataType = "String"),
        @ApiImplicitParam(paramType="query", name = "codes", value = "手牌号数组", required = true, dataType = "Integer[]")
    })
    @RequestMapping(value = "addHandcardBatch", method = RequestMethod.POST)
    @ResponseBody
    public Rest addHandcardBatch(Integer[] codes) {
        //  查询已经存在的手牌号
        QueryWrapper<Handcard> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("code");
        queryWrapper.in("code", codes);
        List<Handcard> handcardList = handcardService.list(queryWrapper);

        //未删除的同编号手牌
        List<Handcard> notDeletedHandcardList = handcardList.stream().filter(item -> item.getIsdelete().equals(0)).collect(Collectors.toList());
        List<Integer> notDeletedCodeList = notDeletedHandcardList.stream().map(item -> item.getCode()).collect(Collectors.toList());

        //删除的同编号手牌
        List<Handcard> deletedHandcardList = handcardList.stream().filter(item -> item.getIsdelete().equals(1)).collect(Collectors.toList());
        List<Integer> deletedCodeList = deletedHandcardList.stream().map(item -> item.getCode()).collect(Collectors.toList());

        for (Integer code: codes){
            if(deletedCodeList.contains(code)){    //  如果已经有被删除的该手牌号，将该手牌设置为未删除
                UpdateWrapper wrapper = new UpdateWrapper();
                wrapper.eq("code", code);
                Handcard handcard = new Handcard();
                handcard.setIsdelete(0);
                handcard.setUpdateTime(new Date());
                handcardService.update(handcard, wrapper);
            } else if (!notDeletedCodeList.contains(code)){ //  添加不存在的手牌号
                Handcard handcard = new Handcard();
                handcard.setCode(code);
                handcardService.save(handcard);
            }
        }
        return RestUtil.rest(RestCodeEnum.CODE_200.getCode(), RestCodeEnum.CODE_200.getDesc());
    }

    @ApiOperation(value = "修改手牌")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", name = "xxxToken", value = "请求头token", required = true, dataType = "String"),
        @ApiImplicitParam(paramType="query", name = "id", value = "表id", required = true, dataType = "Integer"),
        @ApiImplicitParam(paramType="query", name = "code", value = "手牌号", required = true, dataType = "Integer")
    })
    @RequestMapping(value = "updateHandcard", method = RequestMethod.POST)
    @ResponseBody
    public Rest updateHandcard(Integer id, Integer code) {
        QueryWrapper<Handcard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code);
        Handcard handcard = handcardService.getOne(queryWrapper);

        //  如果编号已存在，并且是自己就不做处理了
       if(handcard != null && handcard.getId().equals(id)){

        } else {
            handcard.setId(id);
            handcard.setCode(code);
            handcardService.updateById(handcard);
        }

        return RestUtil.rest(RestCodeEnum.CODE_200.getCode(), RestCodeEnum.CODE_200.getDesc());
    }

    @ApiOperation(value = "删除手牌")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "xxxToken", value = "请求头token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "id", value = "表id", required = true, dataType = "Integer")
    })
    @RequestMapping(value = "deleteHandcard", method = RequestMethod.POST)
    @ResponseBody
    public Rest deleteHandcard(Integer id) {
        Handcard handcard = new Handcard();
        handcard.setId(id);
        handcard.setIsdelete(1);
        handcardService.updateById(handcard);

        return RestUtil.rest(RestCodeEnum.CODE_200.getCode(), RestCodeEnum.CODE_200.getDesc());
    }

}

